#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: case.sh
# Description: 
#
#    animal.sh - 顯示指定的動物叫聲

# Edit History: 
#   2011-05-04    File created.
#========================================================================

#========================================================================
#	-The case statement
#========================================================================

#case word in
#	pattern1)
#		list1
#		;;
#	pattern2)
#		list2
#		;;
#esac

FRUIT=kiwi

#This kind is more verbose
if [ "$FRUIT" = apple ] ; then
	echo "Apple pie is quite tasty."
elif [ "$FRUIT" = banana ] ; then
	echo "I like banana nut bread."
elif [ "$FRUIT" = kiwi ] ; then
	echo "New Zealand is famous for kiwi."
fi

#The real power of the case statement does not lie in simplifying if statements. 
#The power lies in the fact that it uses patterns to perform matching.
case "$FRUIT" in
	apple) 
		echo "Apple pie is quite tasty." 
		;;
	banana) 
		echo "I like banana nut bread." 
		;;
	kiwi) 
		echo "New Zealand is famous for kiwi." 
		;;
esac
#The patterns can use the same special characters as patterns for pathname expansion covered in 
#string "Substitution," along with the or operator, |.
case "$TERM" in
	*term)
		TERM=xterm 
		echo "set to xterm";;
	network|dialup|unknown|vt[0-9][0-9][0-9])
		TERM=vt100 
		echo "set to vt100";;
esac

case `uname` in
	FreeBSD)
	echo "FreeBSD"
	;;
	*)
	echo "Others"
	;;
esac

echo "Enter yes or no."
read yesno  # 從標準輸入讀入一行內容，代入到變數yesno中
case "$yesno" in
    y*|Y*)  # 表y或Y開頭的所有字串
    echo "Your answer is YES."
    ;;  
    [nN]*)  # 表n或N開頭的所有字串
    echo "Your answer is NO."
    ;;  
    *)      # 以上皆不符合的情況
    echo "Your answer is ambigous."
esac

#========================================================================
#    animal.sh - 顯示指定的動物叫聲
#========================================================================
# 若沒有指定選項的話，則會從自身的script名稱去掉副檔名(.sh)者                                                                                 
# 做為選項來執行                                                                                                                              
# 例:cat.sh → animal.sh --cat                                                                                                                 
#     dog.sh → animal.sh --dog                                                                                                                
                                                                                                                                              
# 若沒有指定選項的話，從script名稱去掉副檔名(.sh)                                                                                             
# 做為選項來使用。                                                                                                                            
# 若有指定選項的話，則使用之。                                                                                                                
if [ -z "$1" ]; then                                                                                                                          
    opt=--`basename "$0" .sh`                                                                                                                 
else                                                                                                                                          
    opt="$1"                                                                                                                                  
fi                                                                                                                                            
                                                                                                                                              
# 依據選項來顯示叫聲                                                                                                                          
case "$opt" in                                                                                                                                
    --dog)                                                                                                                                    
    bow="Bowwow, bowwow";;                                                                                                                    
    --cat)                                                                                                                                    
    bow="Meow, meow";;                                                                                                                        
    --cow)                                                                                                                                    
    bow="Moo, moo";;                                                                                                                          
    --pig)                                                                                                                                    
    bow="Oink, Oink";;                                                                                                                        
    *)                                                                                                                                        
    bow="!?X, !?X"                                                                                                                            
esac                                                                                                                                          
echo $bow                                                                                                                                     
